const OSS = require('ali-oss');
const ossConfigJson = require('../../../config/oss.json');

async function getOssInitPrams () {
    let ossParams = {
        region: ossConfigJson.region,
        accessKeyId: ossConfigJson.accessKeyId,
        accessKeySecret: ossConfigJson.accessKeySecret,
        bucket: ossConfigJson.bucketName,
        secure: ossConfigJson.secure
    };
    // if (process.env.NODE_ENV === 'release') {
    //     ossParams.internal = true;
    // }
    return ossParams;
}

async function getBucketClient () {
    return new OSS(await getOssInitPrams());
}

async function uploadFile (objectPath, filePath) {
    let client = await getBucketClient();
    return client.put(objectPath, filePath);
}

async function getObjectSignUrl (objectPath, expire) {
    let client = await getBucketClient();
    return client.signatureUrl(objectPath, {
        expires: expire
    });
}

function getCdnUrl () {
    return ossConfigJson.cdnUrl;
}

module.exports = {
    uploadFile,
    getObjectSignUrl,
    getCdnUrl
};
